/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-11
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description:
 */
import 'package:chat_client/models/user_model.dart';
import 'package:chat_client/providers/application_provider.dart';
import 'package:chat_client/widgets/custom_global_widgets.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class ContactPreviewScreen extends StatefulWidget {
  final User user;
  final String infoSource;
  final bool isBlacked;

  const ContactPreviewScreen({
    super.key,
    required this.user,
    required this.infoSource,
    required this.isBlacked,
  });

  @override
  ContactPreviewScreenState createState() => ContactPreviewScreenState();
}

class ContactPreviewScreenState extends State<ContactPreviewScreen> {
  static const _buttonIconColor = Color(0xFF5A6A91);
  static const _sourceTextStyle = TextStyle(fontSize: 20, color: Colors.grey);
  static const _sourceTitleStyle = TextStyle(fontSize: 22);

  late bool _isBlacked;
  @override
  void initState() {
    super.initState();
    _isBlacked = widget.isBlacked;
  }

  @override
  Widget build(BuildContext context) {
    // 使用listen: false避免不必要的重建
    final model = Provider.of<ApplicationProvider>(context);

    return Scaffold(
      appBar: AppBar(backgroundColor: Colors.white),
      body: Column(
        children: [
          ProfileCard(
            name: widget.user.nickname,
            account: widget.user.username,
            location: widget.user.region,
            avatarUrl: widget.user.avatarUrl,
          ),
          Expanded(child: _buildInfoList(model, context)),
          _buildBottomActionBar(context, model),
        ],
      ),
    );
  }

  Widget _buildInfoList(ApplicationProvider model, BuildContext context) {
    return ListView(
      children: [
        CListTile(
          title: '设置标签',
          icon: Icons.edit_note,
          onTap: () {},
          trailing: const Icon(Icons.chevron_right),
        ),
        Divider(height: 1),
        CListTile(
          titleWidget: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              const Text('来源', style: _sourceTitleStyle),
              Text('来自${widget.infoSource}搜索', style: _sourceTextStyle),
            ],
          ),
          title: '',
        )
      ],
    );
  }

  Widget _buildBottomActionBar(
      BuildContext context, ApplicationProvider model) {
    return SafeArea(
      top: false,
      child: Container(
        color: Colors.white,
        child: _isBlacked
            ? buildBlackedBtu(context, model)
            : buildAddFrienddBtu(context, model),
      ),
    );
  }

  buildBlackedBtu(context, ApplicationProvider model) {
    return Row(
      children: [
        _buildActionButton(
          icon: Icons.block,
          label: '解除拉黑',
          onPressed: () async {
            await model.handleBlackUser(widget.user.userId, true);
            setState(() {
              _isBlacked = false;
            });
          },
        ),
        Container(width: 1, height: 40, color: Colors.grey[300]),
        _buildActionButton(
            icon: Icons.person_add_alt_1,
            label: '添加到通讯录',
            iconColor: Colors.grey),
      ],
    );
  }

  buildAddFrienddBtu(context, ApplicationProvider model) {
    return Row(
      children: [
        _buildActionButton(
          icon: Icons.block,
          label: '加入黑名单',
          onPressed: () async {
            await model.handleBlackUser(widget.user.userId, false);
            setState(() {
              _isBlacked = true;
            });
          },
        ),
        Container(width: 1, height: 40, color: Colors.grey[300]),
        _buildActionButton(
          icon: Icons.person_add_alt_1,
          label: '添加到通讯录',
          onPressed: () => model.openDialogToSendApply(
            userId: widget.user.userId,
            nickname: widget.user.nickname,
          ),
        ),
      ],
    );
  }

  Widget _buildActionButton(
      {required IconData icon,
      required String label,
      VoidCallback? onPressed,
      Color iconColor = _buttonIconColor}) {
    return Expanded(
      child: TextButton.icon(
        icon: Icon(icon, size: 24, color: iconColor),
        label: Text(label),
        onPressed: onPressed,
      ),
    );
  }
}
